package com.ruoyi.web.controller.fx;


import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.fx.FxGoodModel;
import com.ruoyi.system.domain.vo.FxGoodModelMtVo;
import com.ruoyi.system.service.fx.IFxGoodModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 商品模板Controller
 *
 * @author ruoyi
 * @date 2024-09-14
 */
@RestController
@RequestMapping("/fx/model")
public class FxGoodModelController extends BaseController
{
    @Autowired
    private IFxGoodModelService fxGoodModelService;

    /**
     * 查询商品模板列表
     */
    @GetMapping("/list")
    public TableDataInfo list(FxGoodModel fxGoodModel)
    {
        startPage();
        List<FxGoodModel> list = fxGoodModelService.selectFxGoodModelList(fxGoodModel);
        List<Long> modelIds = list.stream().map(FxGoodModel::getId).collect(Collectors.toList());
        FxGoodModel item = new FxGoodModel();
        item.setModelIds(modelIds);
        if (!CollectionUtils.isEmpty(list)){
            List<FxGoodModel> fxGoodModels = fxGoodModelService.selectFxGoodModelSonList(item);
            list.addAll(fxGoodModels);
        }
        return getDataTable(list);
    }

    /**
     * 导出美团商品模板列表
     */
    @Log(title = "导出商品模板列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(FxGoodModel fxGoodModel,HttpServletResponse response ) {
        //复制上面的查询
        List<FxGoodModel> list = fxGoodModelService.selectFxGoodModelList(fxGoodModel);
        List<Long> modelIds = list.stream().map(FxGoodModel::getId).collect(Collectors.toList());
        fxGoodModel.setModelIds(modelIds);
        List<FxGoodModel> fxGoodModels = fxGoodModelService.selectFxGoodModelSonList(fxGoodModel);
        list.addAll(fxGoodModels);
        //将id当做key转换为map
        List<FxGoodModelMtVo> fxGoodModelMtVos = BeanUtil.copyToList(list, FxGoodModelMtVo.class);
        Map<Long, FxGoodModelMtVo> map = fxGoodModelMtVos.stream().collect(Collectors.toMap(FxGoodModelMtVo::getId, t -> t));

        //遍历查询结果
        for (FxGoodModelMtVo fxGoodModelMtVo : fxGoodModelMtVos) {
            //如果是主商品 将规格名称 改为 商品名称
            if (ObjectUtils.isEmpty(fxGoodModelMtVo.getParentId())||fxGoodModelMtVo.getParentId().equals(0L)){
                fxGoodModelMtVo.setSonGoodName(fxGoodModelMtVo.getGoodName());
                //如果是字商品 将商品名称 改为 父商品商品名称
            }else if (map.containsKey(fxGoodModelMtVo.getParentId())){
                FxGoodModelMtVo father = map.get(fxGoodModelMtVo.getParentId());
                String itemGoodName=fxGoodModelMtVo.getGoodName();
                fxGoodModelMtVo.setGoodName(father.getGoodName());
                fxGoodModelMtVo.setSonGoodName(itemGoodName);
            }
        }
        fxGoodModelMtVos.sort(Comparator.comparing(FxGoodModelMtVo::getGoodName));

        ExcelUtil<FxGoodModelMtVo> util = new ExcelUtil<FxGoodModelMtVo>(FxGoodModelMtVo.class);
        util.exportExcel(response, fxGoodModelMtVos, "商品模板列表");
    }

    /**
     * 导入商品模板列表
     */
    @Log(title = "导入商品模板列表", businessType = BusinessType.EXPORT)
    @PostMapping("/importMb")
    public void importMb(@RequestPart MultipartFile file,HttpServletResponse response) {
        fxGoodModelService.importMb(file,response);
    }



    /**
     * 获取商品模板详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(fxGoodModelService.selectFxGoodModelById(id));
    }

    /**
     * 获取商品模板详细信息
     */
    @GetMapping(value = "/model/getModelInfo")
    public AjaxResult getModelInfo(Long id)
    {
        FxGoodModel fxGoodModel = fxGoodModelService.selectFxGoodModelById(id);
        FxGoodModel model = new FxGoodModel();
        List<Long> modelIds = new ArrayList<>();
        modelIds.add(fxGoodModel.getId());
        model.setModelIds(modelIds);
        List<FxGoodModel> fxGoodModels = fxGoodModelService.selectFxGoodModelSonList(model);
        fxGoodModel.setGoodModelList(fxGoodModels);
        return success(fxGoodModel);
    }

    /**
     * 新增或修改商品模板
     */
    @PostMapping
    public AjaxResult save(@RequestBody FxGoodModel fxGoodModel)
    {
        return toAjax(fxGoodModelService.saveGoodModel(fxGoodModel));
    }


    /**
     * 删除商品模板
     */
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(fxGoodModelService.deleteFxGoodModelByIds(ids));
    }


    /**
     * 根据最后一级id来查询模版(不返回id)
     */
    @GetMapping("/hierarchy")
    public AjaxResult queryHierarchy(int hierarchy)
    {
        return AjaxResult.success(fxGoodModelService.queryHierarchy(hierarchy));
    }

    /**
     * 根据层值查询模板信息(返回id)
     * @param tierVal 层值
     * @return
     */
    @GetMapping("/queryModel")
    public  AjaxResult queryModel(Integer tierVal) {
        return AjaxResult.success(fxGoodModelService.queryModel(tierVal));
    }

    @GetMapping("/queryModelName")
    public AjaxResult queryModelName(Long tierVal) {
        return AjaxResult.success(fxGoodModelService.queryModelName(tierVal));
    }


}
